package com.example.knowledgeget1.GraphDBCommon.Service;

import com.example.knowledgeget1.ChatCommon.Entity.NodeType;
import com.example.knowledgeget1.ChatCommon.Repository.NodeTypeRepository;
import com.example.knowledgeget1.GraphDBCommon.Entity.Node;
import com.example.knowledgeget1.GraphDBCommon.Repository.NodeRepository;
import com.example.knowledgeget1.KnowtCommon.Repository.KnowStatusRepository;
import com.example.knowledgeget1.KnowtCommon.Repository.KnowinRepository;
import com.example.knowledgeget1.KnowtCommon.Repository.LikeRepository;
import com.example.knowledgeget1.KnowtCommon.Response.NodeResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NodeService {
    @Autowired
    private NodeRepository nodeRepository;
    @Autowired
    private NodeTypeRepository nodeTypeRepository;
    @Autowired
    private KnowinRepository knowinRepository;
    @Autowired
    private KnowStatusRepository knowStatusRepository;
    @Autowired
    private LikeRepository likeRepository;

    // 存储知识点
    public Node saveNode(Node node) {
        Node node1=nodeRepository.findByName(node.getName());
        Node result;
        if(node1==null){
            System.out.println("Node not found");
            if(node.getId()==null){
                Node maxIdNode=nodeRepository.findNodeWithMaxNodeId();
                Long nowId;
                if(maxIdNode==null){
                    nowId=Long.valueOf(1);
                }else{
                    nowId=maxIdNode.getId()+1;
                }
                node.setId(nowId);
                System.out.println(nowId);
            }
            result= nodeRepository.save(node);
        }else{
            System.out.println("Node already exists");
            node.setId(node1.getId());
            result= nodeRepository.save(node);
        }

        return result;
    }

    // 删除知识点
    public void deleteNode(Node node) {
        String preString=node.getPreKnowledges();
        String generalString=node.getGeneralKnowledges();
        if (preString != null) {
            String[] preList=preString.split("/");
            for (String s : preList) {
                Node node3 = nodeRepository.findByName(s);
                if (node3 != null) {
                    if (nodeRepository.existsPreRelationship(node.getName(), node3.getName())) {
                        nodeRepository.deletePreRelationshipByName(node.getName(), node3.getName());
                    }

                }
            }
        }
        if(generalString!=null){
            String[] generalList=generalString.split("/");
            for (String s : generalList) {
                Node node3 = nodeRepository.findByName(s);
                if (node3 != null) {
                    if (nodeRepository.existsGeneralRelationship(node.getName(), node3.getName())) {
                        nodeRepository.deleteGeneralRelationshipByName(node.getName(), node3.getName());
                    }
                }
            }
        }
        nodeRepository.delete(node);
    }

    // 通过Name删除
    public void deleteByName(String name) {
        NodeType nodeType = nodeTypeRepository.findByName(name);
        Long knowId = nodeType.getId();
        knowinRepository.deleteById_KnowId(knowId);
        System.out.println("success:knowin");
        knowStatusRepository.deleteById_KnowId(knowId);
        System.out.println("success:knowStatus");
        likeRepository.deleteById_KnowId(knowId);
        System.out.println("success:like");
        nodeTypeRepository.deleteById(String.valueOf(knowId));
        System.out.println("success:nodeType");
        Node node=nodeRepository.findByName(name);
        if(node!=null){
            this.deleteNode(node);
        }
    }

    // 查找所有知识点
    public List<Node> findAllNodes(){
        return nodeRepository.findAll();
    }

    // 通过name查找知识点
    public Node findNodeByName(String name) {
        return nodeRepository.findByName(name);
    }

    // 查找两节点之间是否存在pre联系
    public boolean existsPreRelationship(String nodeName, String node2Name) {
        return nodeRepository.existsPreRelationship(nodeName, node2Name);
    }

    // 查找两节点之间是否存在general联系
    public boolean existsGeneralRelationship(String nodeName, String node2Name) {
        return nodeRepository.existsGeneralRelationship(nodeName, node2Name);
    }

    // 添加pre联系
    public void addPreRelationshipById(String nodeName, String node2Name) {
        nodeRepository.addPreRelationshipByName(nodeName, node2Name);
    }

    // 添加general联系
    public void addGeneralRelationshipById(String nodeName, String node2Name) {
        nodeRepository.addGeneralRelationshipByName(nodeName, node2Name);
    }

    // 删除pre联系
    public void deletePreRelationshipByName(String nodeName, String node2Name){
        nodeRepository.deletePreRelationshipByName(nodeName, node2Name);
    }

    // 删除general联系
    public void deleteGeneralRelationshipByName(String nodeName, String node2Name){
        nodeRepository.deleteGeneralRelationshipByName(nodeName, node2Name);
    }

    // 通过名称查找知识点及其前置后置知识点
    public NodeResponse findNodeWithPostKnowledgesByName(String name) {
        Node node = nodeRepository.findByName(name);
        NodeResponse nodeResponse = new NodeResponse();
        if (node != null) {
            nodeResponse.setId(node.getId());
            nodeResponse.setName(node.getName());
            nodeResponse.setIntroduce(node.getIntroduce());
            nodeResponse.setExample(node.getExample());
            nodeResponse.setFunction(node.getFunction());
            nodeResponse.setRecommendation(node.getRecommendation());
            nodeResponse.setGrammar(node.getGrammar());
            nodeResponse.setCharacteristic(node.getCharacteristic());
            nodeResponse.setExercise(node.getExercise());
            List<String> preKnowledgeNames = nodeRepository.findPreKnowledgesByName(name);
            nodeResponse.setPreKnowledges(preKnowledgeNames);
            List<String> postKnowledgeNames = nodeRepository.findPostKnowledgeNamesByName(name);
            nodeResponse.setPostKnowledges(postKnowledgeNames);
        }
        return nodeResponse;
    }

}
